package com.blockmeta.bbs.baselibrary.utils;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by author on 2018/3/29.
 */

public class AnimatorUtil {
    public static ObjectAnimator mObjectAnimator;

    public static void alphaShow(View widget) {
        if (mObjectAnimator == null) {
            mObjectAnimator = ObjectAnimator.ofFloat(widget, "alpha", 0f, 1f);
        }
        mObjectAnimator.setInterpolator(new LinearInterpolator());
        mObjectAnimator.setDuration(200);
        mObjectAnimator.setRepeatCount(0);
        mObjectAnimator.start();
    }

    public static void alphaHide(View widget) {
        if (mObjectAnimator == null) {
            mObjectAnimator = ObjectAnimator.ofFloat(widget, "alpha", 1f, 0f);
        }
        mObjectAnimator.setInterpolator(new LinearInterpolator());
        mObjectAnimator.setDuration(200);
        mObjectAnimator.setRepeatCount(0);
        mObjectAnimator.start();
    }

    public static void rotateStart(View widget) {
        if (mObjectAnimator == null) {
            mObjectAnimator = ObjectAnimator.ofFloat(widget, "rotation", 0f, 360f);
        }
        mObjectAnimator.setInterpolator(new LinearInterpolator());
        mObjectAnimator.setDuration(3500);
        mObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mObjectAnimator.setRepeatMode(ValueAnimator.RESTART);
        mObjectAnimator.start();
    }

    public static void rotateEnd(View widget) {
        if (mObjectAnimator!=null){
            mObjectAnimator.cancel();
            widget.clearAnimation();
        }
    }
    public static void rotatePause(View widget) {
        if (mObjectAnimator!=null){
            mObjectAnimator.pause();
        }
    }

    /**
     * 自定义动画传参
     * @param widget
     */
    public static void rotateStart(View widget,int duration,int count) {
        if (mObjectAnimator == null) {
            mObjectAnimator = ObjectAnimator.ofFloat(widget, "rotation", 0f, 180f);
        }
        mObjectAnimator.setInterpolator(new LinearInterpolator());
        mObjectAnimator.setDuration(duration);
        mObjectAnimator.setRepeatCount(count);
        mObjectAnimator.setRepeatMode(ValueAnimator.RESTART);
        mObjectAnimator.start();
    }


}
